<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link href="/res/third/bootstrap3/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
<script src="/res/common/js/jquery.1.9.1.min.js" type="text/javascript"></script>
<script src="/res/third/artDialog/jquery.artDialog.js" type="text/javascript"></script>
<script src="/res/third/validate/jquery.validate.js" type="text/javascript"></script>
<script type="text/javascript">
	function sosuo() {
		var lpath = location.pathname;
		$("#soform").attr("action", lpath + "?pageNo=" + 1);

		$("#soform").submit();
	}
	
	function selectDownRule(id){
		top.addTempTab("/integral/i_recordrule_list.do?so_pid="+id);
	}
	
</script>
<style type="text/css">
		.excelbtn{display: inline-block;height: 100%;width: 100px;background-color:#38A4C7;color: #fff;text-align: center;line-height: 35px;cursor: pointer;border-radius:5px;}
</style>
</head>

<body>
	<div class="panel panel-info panel-content">
		<div class="panel-heading">
			<button style="float: right" type="button" onclick="addrecordrule('${so_pid!}')" class="btn btn-info">
				<span class="glyphicon glyphicon-plus"></span>
				&nbsp;新增规则
			</button>
			<form id="soform" method="post">
				<input type="hidden" id="so_pid" name="so_pid" value="${so_pid!}">
				<table>
					<tr>
						<td class="w80" style="padding-top: 5px">
							<label for="companyname" class="center-block">规则标题：</label>
						</td>
						<td class="w150 ">
							<input type="text" class="form-control" name="so_title" value="${so_title!}" id="so_search" placeholder="请输入标题" data-options="">
						</td>
						<!--  
						<td class="w80" style="padding-top: 5px; padding-left: 10px">
							<label for="companyname" class="center-block">推荐类型：</label>
						</td>
						<td class="w120 ">
							<select class="form-control" name="so_upToIndex">
							    <option value="" [#if !so_upToIndex?? || so_upToIndex=='']selected="selected"[/#if]>全部</option>
								<option value="0" [#if so_upToIndex?? && so_upToIndex=='0']selected="selected"[/#if]>不推荐</option>
								<option value="1" [#if so_upToIndex?? && so_upToIndex=='1']selected="selected"[/#if]>推荐</option>
							</select>
						</td>
						-->
						<td style="padding-left: 10px">
							<div class="input-group">
								<span class="input-group-btn">
									<button class="btn btn-default" type="button" onclick="sosuo()">搜索</button>
								</span>
							</div>
						</td>
					</tr>
				</table>
			</form>
		</div>
		<table class="table table-hover table-bordered table-striped table-condensed">
			<thead>
				<tr>
					<th width="10%">序号</th>
					<th width="12%">规则标题</th>
					<th width="15%">规则描述</th>
					<th width="12%">积分分值</th>
					<th width="10%">类别</th>
					<th width="10%">分值操作</th>
					<th width="12%">规则编码</th>
					<th width="19%">操作</th>
				</tr>
			</thead>
			[#list pagination.list as rule]
			<tr>
				<td align="center">${rule_index + 1}</td>
				<td align="center">${rule.title!}</td>
				<td align="center">${rule.descr!}</td>
				<td align="center">[#if rule.type==3]无[#else]${rule.scoreval!}[/#if]</td>
				<td align="center">[#if rule.type==1]按分值[#elseif rule.type==3]积分兑换[/#if]</td>
				<td align="center">
					
				[#if rule.operateType ?? && rule.operateType==1]加分[#else]减分[/#if]
				
				</td>
				<td align="center">${rule.ruleCode!}</td>
				<td align="center">
				<!--  -->
					<button type="button" class="btn btn-danger btn-xs delbtn" onclick="edit('${rule.id!}')">编辑</button>
					<button type="button" class="btn btn-danger btn-xs delbtn" onclick="deleteCP('${rule.ruleCode!}')">删除</button>
					<button type="button" class="btn btn-danger btn-xs delbtn" onclick="selectDownRule('${rule.id!}')">查看下级规则</button>
					
				</td>
			</tr>
			[/#list]
		</table>
		<div class="panel-footer">[@fenye p = pagination form = 'infoForm' /]</div>
	</div>
	
	<!-- 添加编辑  -->
	<div id="editoraddView" style="width: 600px; height: 400px; display: none;overflow-x:hidden;overflow-y:scroll;">
		<form id="editoraddform" method="post" class="form-horizontal" role="form">
			<input type="hidden" name="id" id="record_id">
			<div class="form-group">
				<label class="col-sm-2 control-label" id="clickforpro">规则标题</label>
				<div class="col-sm-8">
					<input type="text" class="form-control" placeholder="请输入规则标题"  required="required" name="title" id="addtitle">
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-2 control-label" id="clickforpro">规则描述</label>
				<div class="col-sm-8">
					<input type="text" class="form-control" placeholder="请输入规则描述"  required="required" name="descr" id="addtitle">
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-2 control-label" id="clickforpro">积分分值</label>
				<div class="col-sm-8">
					<input type="number" class="form-control" placeholder="请输积分分值" min="1" required="required" name="score" id="addtitle">
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-2 control-label" id="clickforpro">类别</label>
				<div class="col-sm-8">
					<select name="type" class="form-control">
							<option value="1">按分值</option>
							<option value="3">积分兑换</option>
					</select>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-2 control-label" id="clickforpro">分值操作</label>
				<div class="col-sm-8">
					<select name="operateType" class="form-control">
							<option value="1">加分</option>
							<option value="-1">减分</option>
					</select>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-2 control-label" id="clickforpro">规则编码</label>
				<div class="col-sm-8">
					<input type="text" class="form-control" placeholder="请输入规则编码"  required="required" name="ruleCode" id="addtitle">
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-2 control-label" id="clickforpro">达到模板</label>
				<div class="col-sm-8">
                    <textarea id="container" cols="30" rows="10" class="form-control"></textarea>
					<input type="hidden" id="hasTemplate" name="hasTemplate" >
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-2 control-label" id="clickforpro">未达到模板</label>
				<div class="col-sm-8">
                    <textarea id="container2" cols="30" rows="10" class="form-control"></textarea>
					<input type="hidden" id="disTemplate" name="disTemplate" >
				</div>
			</div>
			<div class="form-group" style="margin-top: 10px;">
				<div class="col-sm-offset-2 col-sm-10">
					<input id="addoreditsub" type="button" onclick="subForm()"  class="btn btn-default" value="提交" />
				</div>
			</div>
			<input type="hidden" name="pid" id="pid">
		</form>
	</div>
	
</body>
</html>
<script type="text/javascript">
	
	//打开编辑窗口
	function edit(id) {
		 clearForm();
		 $.ajax({
				url : '/integral/queryEditrecord.do',
				type : 'post',
				dataType : 'json',
				data :{id:id},
				beforeSend : function() {
					art.dialog.tips("请求中...");
				},
				success : function(data) {
					if (data.success) {
						$("#record_id").val(data.obj.id)
						$("input[name='title']").val(data.obj.title);
						$("input[name='descr']").val(data.obj.descr);
						$("input[name='score']").val(data.obj.score);
						$("input[name='ruleCode']").val(data.obj.ruleCode);
						$('#container').text(data.obj.hasTemplate||'');
                        $('#container2').text(data.obj.disTemplate||'');
						$("select[name='type']").children("option").each(function(){
							if($(this).val()==data.obj.type){
								$(this).attr("selected","selected")
							}
						})
						$("select[name='operateType']").children("option").each(function(){
							if($(this).val()==data.obj.operateType){
								$(this).attr("selected","selected")
							}
						})
					} else {
						error_msg(data.msg, function() {
							location.reload();
						});
					}
				}
			});
			dialog = art.dialog({
				title : "编辑",
				lock : true,
				content : document.getElementById('editoraddView')
		});
	}
	//打开新增窗口
	function addrecordrule(pid){
		clearForm();
		$("#pid").val(pid);
		dialog = art.dialog({
			title : "新增",
			lock : true,
			content : document.getElementById('editoraddView')
		});
	}
	function validate(){
		 var isVal=true;
		 if($("[name='title']").val().trim()==''){
			 alert("标题必须填写")
			 isVal =false;
		 }
		 if($("[name='descr']").val().trim()==''){
			 alert("描述不能为空")
			 isVal =false;
		 }
		 if($("[name='type']").val()=='1'){
			 if($("[name='score']").val()==''){
				 alert("分值不能为空")
				 isVal =false;
			 }
		 }
		 if($("[name='ruleCode']").val().trim()==''){
			 alert("规则编码不能为空");
			 isVal =false;
		 }
		 $("#hasTemplate").val($('#container').val());
		 $("#disTemplate").val($('#container2').val());
		 return isVal;
	}
	function subForm(){
		 if(validate()){
			 if(confirm("确认提交吗")){
				 $.ajax({
						url : '/integral/editOradd.do',
						type : 'post',
						dataType : 'json',
						data :$("#editoraddform").serialize(),
						beforeSend : function() {
							art.dialog.tips("提交中...");
						},
						success : function(data) {
							if (data.success) {
								location.reload();
							} else {
								error_msg(data.msg, function() {
									location.reload();
								});
							}
						}
					});
			 }
		 }
	}
	function clearForm(){
		$("#record_id").val('');
		$("input[name='title']").val('');
		$("input[name='descr']").val("");
		$("input[name='score']").val("");
		$("input[name='ruleCode']").val("");
		
	}
	function deleteCP(ruleCode){
		if(confirm("确认删除吗")){
			 $.ajax({
					url : '/integral/deleteRule.do',
					type : 'post',
					dataType : 'json',
					data :{ruleCode:ruleCode},
					beforeSend : function() {
						art.dialog.tips("提交中...");
					},
					success : function(data) {
						if (data.success) {
							location.reload();
						} else {
							error_msg(data.msg, function() {
								location.reload();
							});
						}
					}
				});
		}
	}
</script>